package impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import dao.BaseDao;
import dao.VisitDao;
import entity.Books;
import entity.Visit;

public class VisitDaoImpl extends BaseDao implements VisitDao {

	@Override
	public int addVisit(Visit visit) {
		String sql = "INSERT visit (id,visit_date,visit_type) VALUES(?,?,?)";
		Object[] objects = {visit.getId(),visit.getVisit_date(),visit.getVisit_type()};
		return execeteUpdata(sql, objects);
	}

	@Override
	public List<Visit> allVisit() {
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		String sql="SELECT * FROM visit";
		List<Visit> visits=new ArrayList<Visit>();
		try {
			conn=getConnection();
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				Visit visit=new Visit();
				visit.setId(rs.getString("id"));
				visit.setVisit_date(rs.getString("visit_date"));
				visit.setVisit_type(rs.getString("visit_type")); 
				visits.add(visit);
			}
		} catch (Exception e) {
		e.printStackTrace();
		}finally{
			closeAll(rs, pstmt, conn);
		}
		return visits;
	}

}
